package com.john.booter.domain.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.GenericGenerator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "t_news")
public class News implements Serializable {
	// ==============
	// PRIVATE FIELDS
	// ==============
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -6948844695642838165L;
	/**
	 * ID
	 */
	@Id
	@GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(columnDefinition = "CHAR(32)")
	private String id;
	/**
	 * 标题
	 */
	@Column(nullable = false)
	private String title;
	/**
	 * 内容
	 */
	@Lob
	@Column(nullable = false)
	private String content;
	/**
	 * 创建时间
	 */
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
//	@JsonFormat(pattern = "yyyy年MM月dd日")
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date createTime;
	/**
	 * 更新时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date updateTime;
	/**
	 * 创建人
	 */
	private String creator;
	/**
	 * 状态
	 */
	private String state;
	
    /**
     * 双向多对多</br>
     * @JoinTable 配置自动生成的表t_news_resources</br>
     * 关联关系：t_news~t_news_resources~t_resources
     */
	@ManyToMany
	@JoinTable(
        name="t_news_file",
        joinColumns=@JoinColumn(name="news_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="file_id", referencedColumnName="id")
    )
//	@JsonManagedReference
	@JsonIgnore
    private List<File> files; 
	
	@PreUpdate
	public void preUpdate() {
		updateTime = new Date();
		//TODO set creator as login user
		creator = "John.Wen";
	}

	@PrePersist
	public void prePersist() {
		Date now = new Date();
		createTime = now;
		updateTime = now;
		
		//TODO set creator as login user
		creator = "John.Wen";
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	// ==============
	// constructor
	// ==============
	public News() {
		// no-args constructor required by JPA spec
		// this one is protected since it shouldn't be used directly
	}

	public News(String id, String title, String content, Date createTime, Date updateTime, String creator, String state,
			List<File> files) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.creator = creator;
		this.state = state;
		this.files = files;
	}

	// ==============
	// Getter and setter methods
	// ==============
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public List<File> getFiles() {
		return files;
	}

	public void setFiles(List<File> files) {
		this.files = files;
	}

}
